package org.o14x.alpha.ui.views.folder;

import java.io.File;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.o14x.alpha.ui.controller.UIController;
import org.o14x.alpha.ui.views.folder.columns.ColumnHelper;

/**
 * Sorts the elements of a specific column.
 * 
 * @author Olivier DANGREAUX
 */
public class Sorter extends ViewerSorter {
	/**
	 * The managed column.
	 */
	private ColumnHelper column;

	/**
	 * The sort order of the column.
	 */
	private int dir;

	/**
	 * UIController
	 */
	private UIController uiController;

	/**
	 * Creates a new instance of Sorter.
	 *
	 * @param column The managed column.
	 * @param dir The sort order (SWT.UP, SWT.DOWN).
	 */
	public Sorter(ColumnHelper column, int dir) {
		super();
		this.column = column;
		this.dir = dir;
	}

	/**
	 * {@inheritDoc}
	 */
	public int compare(Viewer viewer, Object e1, Object e2) {
		int returnValue = column.compare((File) e1, (File) e2);

		if (this.dir == SWT.DOWN) {
			returnValue = returnValue * -1;
		}

		return returnValue;
	}

	/**
	 * Returns the value of uiController.
	 *
	 * @return The value of uiController.
	 */
	public UIController getUiController() {
		return uiController;
	}

	/**
	 * Sets the value of uiController.
	 *
	 * @param uiController The value of uiController to set.
	 */
	public void setUiController(UIController uiController) {
		this.uiController = uiController;
	}
}
